import {
	toast,
	showModal,
	showLoading,
	hideLoading,
	showActionSheet
} from "@/utils/lib/index"
import api from '@/apis'
import store from "@/store"
import router from '@/utils/helpers/hRouter'
import utils from '@/utils/lodash'
import lib from '@/utils/lib/core'
import wb from '@/utils/lib/websocket_copy'
// import wb from '@/utils/lib/websocket'
import {uploadFile} from '@/utils/helpers/upload'
import {mapGetters} from "vuex"


//所有功能函数集合
let h = {
	api,
	toast,
	showModal,
	showLoading,
	hideLoading,
	showActionSheet,
	router,
	lib,
	uploadFile,
	utils,
	wb
}
export default {
	install(Vue) {
		Vue.prototype.$h = h
		Vue.prototype.$store = store
		Vue.mixin({
			computed:{
				...mapGetters(['CustomBar','Custom','StatusBar','token','userInfo','System'])
			},
			methods:{
				goBack(delta = 1, delay) {
					if (delay) {
						setTimeout(() => {
							this.$h.router.back({
								delta
							})
						}, delay)
					} else {
						this.$h.router.back({
							delta
						})
					}
				},
				//全局请求等待函数
				waitingLoading(fn, isShowMsg = false) {
					return async query => {
						showLoading()
						let d = await fn(query)
						//token过期
						if(d.code==10001){
							d = await fn(query)
							hideLoading().then(()=>{
								//是否要显示操作信息
								if(d&&isShowMsg){
									if (d.code == 1) {
										toast(d.msg, true)
									} else {
										toast(d.msg)
									}
								}
							})
							return d
						}else{
							hideLoading().then(()=>{
								//是否要显示操作信息
								if(d&&isShowMsg){
									if (d.code == 1) {
										toast(d.msg, true)
									} else {
										toast(d.msg)
									}
								}
							})
							return d
						}
						
					}
				},
				//全局验证是否授权用户信息函数
				checkUserInfo(){
					// #ifdef MP-WEIXIN
					if(!this.userInfo.avatar||!this.userInfo.nickname){
						this.$h.toast('请先完善个人信息').then(()=>{
							this.$h.router.switchTab({
								path:"/pages/profile/profile",
							})
						})
						return false
					}else{
						return true
					}
					// #endif
					// #ifdef H5
					return true
					// #endif
				}
			}
		})
	}
}
